﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayLeyTree
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        double t1 = 30 * Math.PI / 180;
        double t2 = 20 * Math.PI / 180;
        double pe1 = 0.6;
        double pe2 = 0.7;

        public Form1()
        {
            InitializeComponent();
        }


        private void btnDraw_click(object sender, EventArgs e)
        {
            int n;  int.TryParse(textBox_n.Text, out n);
            double leng; double.TryParse(textBox_leng.Text, out leng);
            double.TryParse(textBox_th2.Text, out t2);
            double.TryParse(textBox_per1.Text, out pe1);
            double.TryParse(textBox_per2.Text, out pe2);
            double t1_n; double.TryParse(textBox_th1.Text, out t1_n);
            t1 = t1_n * Math.PI / 180;
            double t2_n; double.TryParse(textBox_th2.Text, out t2_n);
            t2 = t2_n * Math.PI / 180;


            if (graphics == null) graphics = this.CreateGraphics();
            drawCayLeyTree(n, 200, 310, leng, -Math.PI / 2);
        }

        

        void drawCayLeyTree(int n,double x0,double y0,double leng,double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayLeyTree(n - 1, x1, y1, pe1 * leng, th + t1);
            drawCayLeyTree(n - 1, x1, y1, pe2 * leng, th - t2);
        }

        void drawLine(double x0, double y0, double x1, double y1)
        {
            switch (comboBox_color.Text)
            {
                case "Blue":
                    graphics.DrawLine(Pens.Blue, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Red":
                    graphics.DrawLine(Pens.Red, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Green":
                    graphics.DrawLine(Pens.Green, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Black":
                    graphics.DrawLine(Pens.Black, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Yellow":
                    graphics.DrawLine(Pens.Yellow, (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
            }

            //graphics.DrawLine(Pens.Blue,(int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            graphics.Clear(this.BackColor);
        }
    }
}
